package com.umer.pinban.user;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import com.common.base.activity.BaseActivity;
import com.common.bean.InfoResult;
import com.common.bean.MsgBean;
import com.common.ui.barlibrary.ImmersionBar;
import com.common.utils.EventConstants;
import com.common.utils.SpUtil;
import com.umer.pinban.AppHolder;
import com.umer.pinban.HomeActivity;
import com.umer.pinban.R;
import java.util.Timer;
import java.util.TimerTask;
import butterknife.BindView;

/**
 * 支付成功
 */
public class PaySuccessActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_show)
    TextView tvShow;
    private int count = 5;
    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        //handleMessage为处理消息的方法
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int what = msg.what;
            if (what == 100) {
                count = count - 1;
                if(count>=1){
                    tvShow.setText(""+count);
                }else {
                    /*MsgBean msgBean = new MsgBean();
                    msgBean.setFlag(EventConstants.pay_sucess);
                    getEventBus().post(msg);*/
                    //SpUtil.putInt(PaySuccessActivity.this,"paySucess",1);
                    AppHolder.getInstance().setFlag(1);
                    Intent i = new Intent(PaySuccessActivity.this, HomeActivity.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
                    i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);//设置不要刷新将要跳转的界面
                    startActivity(i);
                }
            }
        }
    };
    private Timer timer = new Timer();
    private TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            Message message = new Message();
            message.what=100;
            handler.sendMessage(message);
        }
    };

    @Override
    public View isSupportLoadSirEnabled() {
        return null;
    }

    @Override
    public boolean isImmersionBarEnabled() {
        return true;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                //是否可以修改导航栏颜色，默认为true
                .navigationBarEnable(true)
                //导航栏颜色，不写默认黑色
                .navigationBarColor(R.color.barcolor)
                .statusBarColor(R.color.colorPrimary)
                //解决状态栏和布局重叠问题，任选其一，默认为false，当为true时一定要指定statusBarColor()，不然状态栏为透明色，还有一些重载方法
                .fitsSystemWindows(true)
                .init();
    }

    @Override
    public boolean isSupportSwipeBackEnabled() {
        return false;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_pay_sucess;
    }

    @Override
    public void init() {
        timer.scheduleAtFixedRate(timerTask,0, 1000);
    }

    @Override
    public void loadData() {

    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
            finish();
            //不执行父类点击事件
            return false;
        }
        //继续执行父类其他点击事件
        return super.onKeyUp(keyCode, event);
    }

    @Override
    protected void onSuccess(int what, InfoResult t) {

    }

    @Override
    protected void onFail(int what, InfoResult t) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopLoopViewPager();
        handler.removeCallbacksAndMessages(null);
    }

    private void stopLoopViewPager() {
        if (timer != null) {
            timer.cancel();
            timer.purge();
            timer = null;
        }
        if (timerTask != null) {
            timerTask.cancel();
            timerTask = null;
        }
    }
}
